package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rules.admin.dao.TreeDedutionDao;
import com.rules.admin.entity.TreeDedution;
import com.rules.admin.service.TreeDedutionService;
import com.rules.admin.service.TreeNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TreeDedutionServiceImpl extends ServiceImpl<TreeDedutionDao, TreeDedution> implements TreeDedutionService {
   @Autowired
   private TreeNodeService treeNodeService;

   public List<TreeDedution> getTreeNodesByTreeId(long treeId) {

      LambdaQueryWrapper<TreeDedution> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(TreeDedution::getTreeId, treeId);
      queryWrapper.orderByAsc(TreeDedution::getCode);
      return this.baseMapper.selectList(queryWrapper);
   }

   public void deleteByTreeId(Long treeId) {
      LambdaQueryWrapper<TreeDedution> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(TreeDedution::getTreeId, treeId);
      this.baseMapper.delete(queryWrapper);
   }

   public void create(TreeDedution treeDedution) {
      treeDedution.setCode(this.treeNodeService.getTreeNodeCode(treeDedution.getNodeId()));
      this.save(treeDedution);
   }

   @Override
   public int countByNodeId(Long parentId) {
      LambdaQueryWrapper<TreeDedution> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(TreeDedution::getNodeId,parentId);
      return this.count(queryWrapper);
   }
}
